package com.chuangan.releasetask.service.impl;

import com.chuangan.releasetask.Utils.PasswordUtil;
import com.chuangan.releasetask.dto.WebDTO;
import com.chuangan.releasetask.enums.ResultEnum;
import com.chuangan.releasetask.exception.ReleaseTaskException;
import com.chuangan.releasetask.mapper.SysConfigMapper;
import com.chuangan.releasetask.mapper.TaskMapper;
import com.chuangan.releasetask.mapper.UserMapper;
import com.chuangan.releasetask.pojo.BonusRecord;
import com.chuangan.releasetask.pojo.SysConfig;
import com.chuangan.releasetask.pojo.TaskMaster;
import com.chuangan.releasetask.pojo.User;
import com.chuangan.releasetask.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * @Author: jinLong
 * @Date: 2020/8/13 14:13
 * @Version 1.0
 */

@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SysConfigMapper sysConfigMapper;



    /**
     * 功能描述: APP注册
     * @Return: void
     * @Author: jinLong
     * @Date: 2020/8/29 14:13
     */
    @Override
    public void register(WebDTO webDTO) {
        if (webDTO.getCode().equals(redisTemplate.opsForValue().get(webDTO.getUserAccount()+"code"))){
            System.out.println("pppp");
            userMapper.register(webDTO);
        }else {
            throw new ReleaseTaskException(ResultEnum.PARAM_ERROR);
        }

    }

    /**
     * 功能描述: 根据手机号查询是否已经注册
     * @Return: com.chuangan.releasetask.pojo.User
     * @Author: jinLong
     * @Date: 2020/8/14 14:59
     */
    @Override
    public User findOneUser(String userAccount) {
        return userMapper.findOneUser(userAccount);
    }

    /**
     *
     * 功能描述: 登录
     * @Return: com.chuangan.releasetask.pojo.User
     * @Author: jinLong
     * @Date: 2020/8/18 15:09
     */
    @Override
    public User login(User user) {
        User loginUser=userMapper.login(user.getUserAccount());
        if (loginUser!=null&& loginUser.getUserAccount().equals(user.getUserAccount()) && PasswordUtil.verify(user.getUserPassword(),loginUser.getUserPassword())){
            return loginUser;
        }
        return null;
    }


    /**
     *
     * 功能描述: 修改登录状态
     * @Return: void
     * @Author: jinLong
     * @Date: 2020/8/20 17:03
     */
    @Override
    public void updateStatus(String userAccount) {
        userMapper.updateStatus(userAccount);
    }

    @Override
    public void getTaskFee4Account(String taskID, String userAccount) {
        TaskMaster taskMaster = taskMapper.selectTaskById(taskID);
        User user = userMapper.findOneUser(userAccount);
        BonusRecord bonusRecord = new BonusRecord();
        if(user.getUserEstate()==1){
            user.setUserBalance(user.getUserBalance().add(taskMaster.getTaskFee()));
            userMapper.creditCal(user.getUserBalance(),userAccount);
            bonusRecord.setUserAccount(userAccount);
            bonusRecord.setBonusName("任务奖励金");
            bonusRecord.setBonus(taskMaster.getTaskFee());
            userMapper.addBonusDrawRecord(bonusRecord);
        }
        else if(user.getUserEstate()==2)
        {
            SysConfig sysConfig_Lv1 = sysConfigMapper.getSysConfigByName("一级系数");
            User parentUser = userMapper.findOneUser(user.getUserInvitor());
            userMapper.creditCal(user.getUserBalance().add(taskMaster.getTaskFee()),userAccount);
            bonusRecord.setUserAccount(userAccount);
            bonusRecord.setBonusName("任务奖励金");
            bonusRecord.setBonus(taskMaster.getTaskFee());
            userMapper.addBonusDrawRecord(bonusRecord);
            userMapper.creditCal(parentUser.getUserBalance().add(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv1.getParamValue()))),parentUser.getUserAccount());
            bonusRecord.setUserAccount(parentUser.getUserAccount());
            bonusRecord.setBonusName("一级任务奖励金");
            bonusRecord.setFromAccount(userAccount);
            bonusRecord.setBonus(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv1.getParamValue())));
            userMapper.addBonusDrawRecord(bonusRecord);
        }
        else if(user.getUserEstate()==3){
            User parentUser = userMapper.findOneUser(user.getUserInvitor());
            User topUser = userMapper.findOneUser(parentUser.getUserInvitor());
            SysConfig sysConfig_Lv1 = sysConfigMapper.getSysConfigByName("一级系数");
            SysConfig sysConfig_Lv2 = sysConfigMapper.getSysConfigByName("二级系数");
            userMapper.creditCal(user.getUserBalance().add(taskMaster.getTaskFee()),userAccount);
            bonusRecord.setUserAccount(userAccount);
            bonusRecord.setBonusName("任务奖励金");
            bonusRecord.setBonus(taskMaster.getTaskFee());
            userMapper.addBonusDrawRecord(bonusRecord);
            userMapper.creditCal(parentUser.getUserBalance().add(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv1.getParamValue()))),parentUser.getUserAccount());
            bonusRecord.setUserAccount(parentUser.getUserAccount());
            bonusRecord.setBonusName("一级任务奖励金");
            bonusRecord.setFromAccount(userAccount);
            bonusRecord.setBonus(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv1.getParamValue())));
            userMapper.addBonusDrawRecord(bonusRecord);
            userMapper.creditCal(topUser.getUserBalance().add(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv2.getParamValue()))),topUser.getUserAccount());
            bonusRecord.setUserAccount(topUser.getUserAccount());
            bonusRecord.setBonusName("二级任务奖励金");
            bonusRecord.setFromAccount(userAccount);
            bonusRecord.setBonus(taskMaster.getTaskFee().multiply(new BigDecimal(sysConfig_Lv2.getParamValue())));
            userMapper.addBonusDrawRecord(bonusRecord);
        }
    }

    /**
     * 功能描述: 更新用户余额
     * @Return: void
     * @Author: jinLong
     * @Date: 2020/9/17 16:06
     */
    @Override
    public void updateBalance(BigDecimal sumBalance,String userAccount) {
        userMapper.updateBalance(sumBalance,userAccount);
    }

    @Override
    public List<Map>  selectUserTask(WebDTO webDTO) {
        return userMapper.selectUserTask(webDTO);
    }
}
